home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 295_01 / bsync.c < prev    next >
Text File  |  1989-12-28  |  2KB  |  104 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "bsync.c    1.2 - 89/10/31" */
  5.  
  6. #include <errno.h>
  7. /*#include <stddef.h>*/
  8. #include "blkio_.h"
  9.  
  10. /*man---------------------------------------------------------------------------
  11. NAME
  12.      bsync - synchronize a block file
  13.  
  14. SYNOPSIS
  15.      #include <blkio.h>
  16.  
  17.      int bsync(bp)
  18.      BLKFILE *bp;
  19.  
  20. DESCRIPTION
  21.      The bsync function causes the block file associated with BLKFILE
  22.      pointer bp to be synchronized with its buffers; any buffered data
  23.      which has been written to the buffers is written to the file.
  24.      The header, if it has been modified, is written out last.  The
  25.      block file remains open and the buffers retain their contents.
  26.      If bp is open read-only or is not buffered, there will be no data
  27.      to synchronize and bsync will return a value of zero immediately.
  28.  
  29.      bsync will fail if one or more of the following is true:
  30.  
  31.      [EINVAL]       bp is not a valid BLKFILE pointer.
  32.      [BENOPEN]      bp is not open.
  33.  
  34. SEE ALSO
  35.      bexit, bflush, bputb.
  36.  
  37. DIAGNOSTICS
  38.      Upon successful completion, a value of 0 is returned.  Otherwise,
  39.      a value of -1 is returned, and errno set to indicate the error.
  40.  
  41. ------------------------------------------------------------------------------*/
  42. int bsync(bp)
  43. BLKFILE *bp;
  44. {
  45.     int i = 0;
  46. #ifdef DEBUG
  47.     bpos_t endblk = 0;
  48. #endif
  49.  
  50.     /* validate arguments */
  51.     if (!b_valid(bp)) {
  52.         errno = EINVAL;
  53.         return -1;
  54.     }
  55.  
  56.     /* check if not open */
  57.     if (!(bp->flags & BIOOPEN)) {
  58.         errno = BENOPEN;
  59.         return -1;
  60.     }
  61.  
  62.     /* check if not open for writing */
  63.     if (!(bp->flags & BIOWRITE)) {
  64.         errno = 0;
  65.         return 0;
  66.     }
  67.  
  68.     /* check if not buffered */
  69.     if (bp->bufcnt == 0) {
  70.         errno = 0;
  71.         return 0;
  72.     }
  73.  
  74.     /* synchronize block in each buffer */
  75.     for (i = 1; i <= bp->bufcnt; i++) {
  76.         if (b_put(bp, (size_t)i) == -1) {
  77.             BEPRINT;
  78.             return -1;
  79.         }
  80.     }
  81.  
  82.     /* synchronize header */
  83.     if (b_put(bp, (size_t)0) == -1) {
  84.         BEPRINT;
  85.         return -1;
  86.     }
  87.  
  88. #ifdef DEBUG
  89.     /* check block count */
  90.     if (b_uendblk(bp, &endblk) == -1) {
  91.         BEPRINT;
  92.         return -1;
  93.     }
  94.     if (bp->endblk != endblk) {
  95.         BEPRINT;
  96.         errno = BEPANIC;
  97.         return -1;
  98.     }
  99. #endif
  100.  
  101.     errno = 0;
  102.     return 0;
  103. }
  104.